/**
 * 手机端picker组件
 * Created by KY on 2017/8/15
 **/
define(['jquery-weui'], function () {
    return LDFW.Component.extend({
        initialize: function () {
            const _this = this
            // this.$el.hide()
            // let $select = $('<span class="select-select"><span class="select-select__rendered"></span></span>')
            //     $select.insertAfter(this.$el)
            let defaultValue = this.defaultValue = this.$el.attr('value')||this.$el.val(),
                bindData = this.$el.attr('ld-bind-data'),
                bindDict = this.$el.attr('ld-bind-dict')
            // if(placeholder){
            //     $select.find('.select-select__rendered')
            //         .html('<span class="select-select__placeholder">'+placeholder+'</span>')
            // }
            // this.$select = $select
            if(bindData){
                let data = this.parent.model.get(bindData),
                    labelName = this.$el.attr('ld-label-field'),
                    valueName = this.$el.attr('ld-value-field')
                let values = [],
                    displayValues = []
                data.forEach(function(item){
                    values.push(item[valueName])
                    displayValues.push(item[labelName])
                })
                this.setDataItems({textAlign:'center',values:values,displayValues:displayValues})
            }
            if(bindDict){
                this.setDictItems(dict.getDictItems(bindDict))
                dict.on('change', function () {
                    _this.setDictItems(dict.getDictItems(bindDict))
                })
            }
        },
        events:{
            'click':'open',
            'blur':'close',
        },
        open:function(){
            this.$el.picker('open')
        },
        close:function(){
            this.$el.picker("close")
        },
        setValue:function(item){

        },
        getValue:function(){
            return this.$el.attr('data-values')
        },
        setDataItems:function(data){
            this.$el.picker({
                title: "请选择",
                cols: [
                    data
                ],
                formatValue:function(picker,value,displayValue){
                    return displayValue
                },
                onClose:function(picker){
                    picker.input.attr('data-values',picker.value[0])
                }
            })
        },
        setDictItems: function (data) {
            const _this = this
            let selectItems = [],
                selectOption = {},
                defaultValue = this.defaultValue,
                defaultValueIndex = 0
            try {
                if (defaultValue) {
                    defaultValue = JSON.parse(defaultValue)
                }
            } catch (e) {
                defaultValue = defaultValue
            }
            _.each(data, function (title, value) {
                let item = {title: title, value: value}
                if(!_.contains(_this.omitValue,value)){
                    selectItems.push(item)
                    if(defaultValue == value){
                        defaultValueIndex = selectItems.length-1
                    }
                }
            })
            this.setValue(selectItems[defaultValueIndex])
            this.$el.select('update',{
                items:selectItems,
                onClose:function(select){
                    _this.trigger('change',select.data)
                }
            })
        }
    })
})